import cherrypy
from string import Template

DEXEN_INFO_PAGE = """
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
    <HEAD>
      <TITLE>Dexen on Amazon EC2</TITLE>
    </HEAD>
    <BODY>
        <H1>DEXEN LOG FILES</H1>
        <table border="1" width="100%">
            <col width="50%">
            <col width="50%">
            <tr>
                <td valign="top">
                    <H2>Log file for dexen update</H2>
                    $UPDATE_LOG
                </td>
                <td valign="top">
                    ---
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <H2>Log file for dexen server</H2>
                    $SERVER_LOG
                </td>
                <td valign="top">
                    <H2>Log file for dexen server job</H2>
                    $SERVER_JOB_LOG
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <H2>Log file for dexen master</H2>
                    $MASTER_LOG
                </td>
                <td valign="top">
                    <H2>Log file for dexen master job</H2>
                    $MASTER_JOB_LOG
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <H2>Log file for cherrypy webserver</H2>
                    $WEBSERVER_LOG
                </td>
                <td valign="top">
                    <H2>Log file for cherrypy webserver job</H2>
                    $WEBSERVER_JOB_LOG
                </td>
            </tr>
        </table>
   </BODY>
</HTML>
"""

class DexenInfoPage:
    def index(self):
        t = Template(DEXEN_INFO_PAGE)
        

        try:
            f = open('/var/log/upstart/dexen-update-job.log', 'r')
            update_log = "<p>" + ("</p><p>".join(f.readlines())) + "</p>"
            f.close()
        except:
            update_log = "File could not be read"
        

        
        try:
            f = open('/var/log/upstart/dexen-server.log', 'r')
            server_log = "<p>" + ("</p><p>".join(f.readlines())) + "</p>"
            f.close()
        except:
            server_log = "File could not be read"
            
        try:
            f = open('/var/log/upstart/dexen-server-job.log', 'r')
            server_job_log = "<p>" + ("</p><p>".join(f.readlines())) + "</p>"
            f.close()
        except:
            server_job_log = "File could not be read"
            
        try:
            f = open('/var/log/upstart/dexen-masters.log', 'r')
            master_log = "<p>" + ("</p><p>".join(f.readlines())) + "</p>"
            f.close()
        except:
            master_log = "File could not be read"

        try:
            f = open('/var/log/upstart/dexen-masters-job.log', 'r')
            master_job_log = "<p>" + ("</p><p>".join(f.readlines())) + "</p>"
            f.close()
        except:
            master_job_log = "File could not be read"
            
        try:
            f = open('/var/log/upstart/dexen-webserver.log', 'r')
            webserver_log = "<p>" + ("</p><p>".join(f.readlines())) + "</p>"
            f.close()
        except:
            webserver_log = "File could not be read"
            
        try:
            f = open('/var/log/upstart/dexen-webserver-job.log', 'r')
            webserver_job_log = "<p>" + ("</p><p>".join(f.readlines())) + "</p>"
            f.close()
        except:
            webserver_job_log = "File could not be read"
            
        return t.substitute(
            UPDATE_LOG = update_log, 
            SERVER_LOG = server_log, 
            SERVER_JOB_LOG = server_job_log, 
            MASTER_LOG = master_log, 
            MASTER_JOB_LOG = master_job_log,
            WEBSERVER_LOG = webserver_log,
            WEBSERVER_JOB_LOG = webserver_job_log
            )
    index.exposed = True
    
cherrypy.config.update({'server.socket_host': '0.0.0.0',
                        'server.socket_port': 8080,
                       })
cherrypy.quickstart(DexenInfoPage())